import {
  toast
} from "../../utils/util";

// vip/enterprise/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    record: [],
    idisplaylength: 10,
    idisplaystart: 0,
    compony: '',
    business: '',
    ent: 1,
    total: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.globalData.imgUrl = []
  },
  getRecord() {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 2000
    })
    const result = this.data.record
    const {
      idisplaylength,
      idisplaystart
    } = this.data
    const state = ''
    app.$api.apply.enterpriseFind({
      idisplaylength,
      idisplaystart,
      state
    }).then((res) => {
      const data = res.data
      wx.hideToast()
      const newArray = result.concat(data.data)
      this.setData({
        record: newArray
        // total: data.total
      })
    }).catch((res) => {
      wx.hideToast()
      toast('请稍后再试', 1000)
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.globalData.imgUrl = []
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // const total = this.data.total
    // const record = this.data.record.length
    // if (record && record === total) {
    //   toast("没有更多", 1000)
    //   return
    // }
    // this.setData({
    //   idisplaystart: record
    // })
    // this.getRecord()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindDelete(e) {
    const record = this.data.record
    const {
      id,
      index
    } = e.detail
    app.$api.apply.enterpriseDelete({
        id
      })
      .then((res) => {
        record.splice(index, 1)
        this.setData({
          record: record
        })
        toast('删除成功', 1000)
      }).catch((res) => {
        toast('请稍后再试', 1000)
      })
  },
  bindSubmit(e) {
    const flag = this.validate()
    const record = this.data.record
    if (flag) {
      const fronturl = JSON.parse(app.globalData.imgUrl.join('')).fileUrl
      const entname = this.data.compony
      const businessno = this.data.business
      if (record.length) {
        app.$api.apply.enterpriseUpdate({
          id: record[0].id,
          fronturl,
          entname,
          businessno
        }).then((res) => {
          toast("提交成功", 1000)
          this.addItem()
        }).catch((res) => {
          toast('请稍后再试', 1000)
        })
      } else {
        app.$api.apply.enterpriseAdd({
          fronturl,
          entname,
          businessno
        }).then((res) => {
          toast("提交成功", 1000)
          this.addItem()
        }).catch((res) => {
          toast('请稍后再试', 1000)
        })
      }
    }
  },
  addItem() {
    const idisplaylength = 1
    const idisplaystart = 0
    const state = ''
    const result = [...this.data.record]
    app.$api.apply.enterpriseFind({
      idisplaylength,
      idisplaystart,
      state
    }).then((res) => {
      const data = res.data
      result.unshift(...data.data)
      console.log(result)
      this.setData({
        record: result,
        total: data.total
      })
      console.log(this.data.record, this.data.total)
    }).catch((res) => {
      wx.hideToast()
      toast('请稍后再试', 1000)
    })
  },
  validate() {
    const {
      compony,
      business
    } = this.data
    const imgUrl = app.globalData.imgUrl
    console.log(imgUrl.length)
    if (!imgUrl.length) {
      toast("照片不能为空", 1000)
      return false
    }
    if (compony.trim() === '') {
      toast("公司名称不能为空", 1000)
      return false
    }
    if (business.trim() === 0) {
      toast("营业执照不能为空", 1000)
      return false
    }
    return true
  },
  handleCompony(e) {
    const value = e.detail.value
    this.setData({
      compony: value
    })
  },
  handleBusiness(e) {
    const value = e.detail.value
    this.setData({
      business: value
    })
  },
})